//
//  UploadStasticManager.swift
//  EasyDana
//
//  Created by mac on 2024/10/19.
//

import Foundation
import AdSupport
import CoreLocation
class UploadStasticManager {
    static let `default` = UploadStasticManager()
    
    func uploadDataWhenLaunchAndHome(home: Bool) {
        LocationManager.default.uploadLocation(showAlert: home)
        self.uploadGoogleMarket()
        self.uploadDeviceInfo()
    }
    
    private func uploadGoogleMarket() {
        Task {
            do {
                let idfv = DeviceManager.defalult.idfv
                let idfa = await DeviceManager.defalult.idfa()
                try await HTTPManager.default.post(path: Api.uploadGoogleMarket, param: ["swift": idfv, "skinned": idfa], dataType: String.self)
            } catch {
                
            }
        }
    }
    
    private func uploadDeviceInfo() {
        Task {
            do {
                var param: [String: Any] = [:]
                let gamble = [
                    "manipulate": DeviceManager.defalult.availableDiskSize,
                    "croupier": DeviceManager.defalult.totalDiskSize,
                    "assured": DeviceManager.defalult.totalMemorySize,
                    "remainder": DeviceManager.defalult.availableMemorySize
                ]
                param["gamble"] = gamble
                let fled = ["informer": DeviceManager.defalult.chargeLevel, "youngster": DeviceManager.defalult.charging]
                param["fled"] = fled
                let makee = [
                    "pidgin": DeviceManager.defalult.systemVersion,
                    "hi": DeviceManager.defalult.brand,
                    "ringleaders": DeviceManager.defalult.originPhoneModel
                ]
                param["makee"] = makee
                let surprisingly = ["forearm": DeviceManager.defalult.isSimulator, "slides": DeviceManager.defalult.isJailBroken]
                param["surprisingly"] = surprisingly
                let networkType = await DeviceManager.defalult.getNetworkType()
                let idfa = await DeviceManager.defalult.idfa()
                let palm = ["rasps": DeviceManager.defalult.timeZone, "swift": DeviceManager.defalult.idfv, "counted": DeviceManager.defalult.language, "silk": networkType, "skinned": idfa]
                param["palm"] = palm
                let connectedWifiInfo = DeviceManager.defalult.getConnectedWifiInfo()
                var mac = ""
                if let m = connectedWifiInfo["BSSID"] as? String {
                    mac = m
                }
                var ssid = ""
                if let s = connectedWifiInfo["SSID"] as? String {
                    ssid = s
                }
                let matting = ["cowries": mac, "th": ssid]
                param["forcibly"] = ["matting": matting]
                let data = try JSONSerialization.data(withJSONObject: param)
                let str = String(data: data, encoding: .utf8) ?? ""
                try await HTTPManager.default.post(path: Api.uploadDeviceInfo, param: ["belles": str], dataType: String.self)
            } catch {
                
            }
        }
    }
    
    func uploadRiskPoint(type: Int, beginTime: Date?, orderNo: String = "")  {
        Task {
            do {
                var param: [String: Any] = [:]
                param["mythan"] = type
                let uncovered = 2
                param["uncovered"] = uncovered
                let pice = DeviceManager.defalult.idfv
                param["pice"] = pice
                let endDate = Date()
                let deposit = await DeviceManager.defalult.idfa()
                param["deposit"] = deposit
                var location = CLLocation(latitude: 0, longitude: 0)
                do {
                    location = try await LocationManager.default.getLocation()
                } catch {
        
                }
                let laputa = location.coordinate.longitude
                param["laputa"] = laputa
                let annas = location.coordinate.latitude
                param["annas"] = annas
                if let beginTime = beginTime {
                    let progresses = beginTime.timeIntervalSince1970
                    param["progresses"] = progresses
                }
                let game = endDate.timeIntervalSince1970
                param["game"] = game
                if orderNo.count > 0 {
                    param["innocents"] = orderNo
                }
                try await HTTPManager.default.post(path: Api.uploadRiskPoint, param: param, dataType: [Int].self)
            } catch {
            }
        }
    }
}
